package com.dianshan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dianshan.entity.User;
import com.dianshan.mapper.UserMapper;
import com.dianshan.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.domain.PageQuery;
import com.domain.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author xiaolong
 * @since 2024-06-18
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public PageResult<User> listOne(PageQuery pageQuery) {

        Page<User> pageInfo = pageQuery.toMpPage();

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(StrUtil.isNotEmpty(pageQuery.getName()),User::getUsername,pageQuery.getName());

        Page<User> page = userMapper.selectPage(pageInfo, wrapper);

        return PageResult.of(page,user -> BeanUtil.copyProperties(user,User.class));

    }
}
